package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.commonutils.R;
import com.guigu.eduservice.entity.EduTeacher;
import com.guigu.eduservice.entity.vo.TeacherQuery;
import com.guigu.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: janecraft
 * @Date: 2022/5/31
 * @Description: com.guigu.eduservice.controller
 * @Version： 1.0
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher/")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    // 查询所有
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> eduTeacherList = eduTeacherService.list(null);
        return R.ok().data("teacherList", eduTeacherList);
    }

    // 新增
    @ApiOperation(value = "新增讲师")
    @PostMapping("/insert")
    public R insertTeacher(
            @ApiParam(name = "eduTeacher", value = "新增讲师对象数据", required = true)
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if(flag)
            return R.ok();
        return R.error();
    }

    // 删除
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("/{id}")
    public R deleteTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        boolean flag = eduTeacherService.removeById(id);
        if(flag)
            return R.ok();
        return R.error();
    }

    // 修改
    @PostMapping
    public R updateTeacherById(@RequestBody EduTeacher teacher ) {
        boolean flag = this.eduTeacherService.updateById(teacher);
        if(flag)
            return R.ok();
        return R.error();
    }

    // 带条件查询的分页
    @PostMapping("/page/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current,
                         @PathVariable Long limit,
                         @RequestBody TeacherQuery teacherQuery) {
        Page<EduTeacher> page = new Page<>(current, limit);

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(teacherQuery != null) {
            if(!StringUtils.isEmpty(teacherQuery.getSearch_name()))
                wrapper.like("name", teacherQuery.getSearch_name());
            if(!StringUtils.isEmpty(teacherQuery.getSearch_begin()))
                wrapper.ge("gmt_create", teacherQuery.getSearch_begin());
            if(!StringUtils.isEmpty(teacherQuery.getSearch_end()))
                wrapper.lt("gmt_create", teacherQuery.getSearch_end());
        }
        this.eduTeacherService.page(page, wrapper);
        return R.ok().data("total", page.getTotal())
                .data("rows", page.getRecords())
                .data("current", page.getCurrent())
                .data("limit", page.getSize());
    }


}
